package com.ruoyi.exam.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.exam.vo.ExamRandomPracticeQuestionVo;
import com.ruoyi.exam.service.IExamRandomPracticeQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 练习题目Controller
 *
 * @author Tellsea
 * @date 2025-07-28
 */
@Api(tags = "练习题目Controller")
@RestController
@RequestMapping("/exam/randomPracticeQuestion")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamRandomPracticeQuestionController extends BaseController {

    private final IExamRandomPracticeQuestionService examRandomPracticeQuestionService;

    @ApiOperation("查询练习题目列表")
    @PreAuthorize("@ss.hasPermi('exam:randomPracticeQuestion:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamRandomPracticeQuestionVo> list(ExamRandomPracticeQuestionVo entity) {
        return examRandomPracticeQuestionService.queryList(entity);
    }

    @ApiOperation("查询练习题目所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamRandomPracticeQuestionVo entity) {
        return AjaxResult.success("查询成功", examRandomPracticeQuestionService.queryAll(entity));
    }

    @ApiOperation("导出练习题目列表")
    @PreAuthorize("@ss.hasPermi('exam:randomPracticeQuestion:export')")
    @Log(title = "练习题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamRandomPracticeQuestionVo entity) {
        List<ExamRandomPracticeQuestionVo> list = examRandomPracticeQuestionService.queryAll(entity);
        ExcelUtil<ExamRandomPracticeQuestionVo> util = new ExcelUtil<>(ExamRandomPracticeQuestionVo.class);
        util.exportExcel(response, list, "练习题目数据");
    }

    @ApiOperation("获取练习题目详细信息")
    @PreAuthorize("@ss.hasPermi('exam:randomPracticeQuestion:query')")
    @GetMapping(value = "/getInfo/{practiceQuestionId}")
    public AjaxResult getInfo(@PathVariable("practiceQuestionId") Long practiceQuestionId) {
        return AjaxResult.success("查询成功", examRandomPracticeQuestionService.queryById(practiceQuestionId));
    }

    @ApiOperation("新增练习题目")
    @PreAuthorize("@ss.hasPermi('exam:randomPracticeQuestion:add')")
    @Log(title = "练习题目", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamRandomPracticeQuestionVo entity) {
        return toAjax(examRandomPracticeQuestionService.save(entity));
    }

    @ApiOperation("修改练习题目")
    @PreAuthorize("@ss.hasPermi('exam:randomPracticeQuestion:edit')")
    @Log(title = "练习题目", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamRandomPracticeQuestionVo entity) {
        return toAjax(examRandomPracticeQuestionService.updateById(entity));
    }

    @ApiOperation("删除练习题目")
    @PreAuthorize("@ss.hasPermi('exam:randomPracticeQuestion:remove')")
    @Log(title = "练习题目", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{practiceQuestionIds}")
    public AjaxResult remove(@PathVariable Long[] practiceQuestionIds) {
        return toAjax(examRandomPracticeQuestionService.removeByIds(Arrays.asList(practiceQuestionIds)) ? 1 : 0);
    }
}
